import { _decorator, Component, Node,AudioClip,game,AudioSource,tween ,resources} from 'cc';

const { ccclass, property } = _decorator;

export const AudioClipID=
{
    Coin:"coin",
    Tip:"tip",
    Exchange:"exchange",
    Bomb:"bomb",
    Explode:"explode",
    Rocket:"rocket",
    LevelPass:"levelPass",
    Error:"error"
};

//Audio音频管理器
@ccclass('AudioManager')
export class AudioManager extends Component {

    private audioSource:AudioSource=null;

    public static instance:AudioManager=null;

    public static sndOn:boolean=true;
    public static musicOn:boolean=true;

    public static shouldResumeBgm:boolean=false;

    public static keyClips={};
    public static keyClipsCount:number=9;
    public static keyClipsLoaded:boolean=false;

    public static clips={};
    public static clipsCount:number=8;
    public static clipsLoaded:boolean=false;

    public static startLoading:boolean=false;

    onLoad()
    {
        AudioManager.instance=this;
        this.audioSource=this.node.getComponent(AudioSource);
        game.addPersistRootNode(this.node);
    }

    start()
    {
        //this.loadKeyClips();
        //this.loadNewBrickClips();
    }

    update(dt:number)
    {
        if(!AudioManager.startLoading)
        {
            AudioManager.startLoading=true;
            this.loadClips();
            this.loadKeyClips();
        }
    }

    loadKeyClips()
    {
        resources.loadDir("snd/key", AudioClip,(err, clips)=>{
            for(let i=0;i<clips.length;i++)
            {
                AudioManager.keyClips[clips[i].name]=clips[i];
                //console.log("** loaded clip="+clips[i].name);
            }
            AudioManager.keyClipsLoaded=true;
        });
    }

    loadClips()
    {
        resources.loadDir("snd/clips", AudioClip,(err, clips)=>{
            for(let i=0;i<clips.length;i++)
            {
                AudioManager.clips[clips[i].name]=clips[i];
            }
            AudioManager.clipsLoaded=true;
        });
    }

    onDisable()
    {
        //this.audioSource.clip.stop();
    }

    onEnable()
    {

        //this.audioSource.clip.play();
    }

    public static onHide()
    {
        if(AudioManager.instance===null)
            return;
        if(AudioManager.instance.audioSource.playing)
        {
            AudioManager.shouldResumeBgm=true;
            AudioManager.instance.audioSource.pause();
        }
    }

    public static onShow()
    {
        if(AudioManager.instance===null)
            return;
        if(AudioManager.shouldResumeBgm)
        {
            AudioManager.instance.scheduleOnce(()=>{
                AudioManager.instance.audioSource.play();
                AudioManager.shouldResumeBgm=false;
            }, 0.5);
        }
    }

    //播放音效
    public static playClip(acId:string, vol:number=1)
    {
        if(!AudioManager.clipsLoaded)
            return;
        if(AudioManager.sndOn)
        {
            AudioManager.instance.audioSource.playOneShot(AudioManager.clips[acId], vol);
        }
    }

    public static playKeyClip(idx:number)
    {
        if(!AudioManager.keyClipsLoaded)
            return;
        if(idx<0)
            idx=0;
        if(idx>AudioManager.keyClipsCount-1)
            idx=AudioManager.keyClipsCount-1;
        if(AudioManager.sndOn)
        {
            let clipName:string="c"+ idx;
            AudioManager.instance.audioSource.playOneShot(AudioManager.keyClips[clipName], 1);
        }
    }

    //播放背景音乐
    public static playBgm()
    {
        //console.log("** musicOn="+AudioManager.musicOn);
        if(!AudioManager.musicOn)
            return;

        if(!AudioManager.instance.audioSource.playing)
            AudioManager.instance.audioSource.play();
    }

    //背景音乐淡入淡出
    public static fadeoutBgm()
    {
        if(AudioManager.instance.audioSource.playing)
        {
            tween(AudioManager.instance.audioSource)
                .to(0.5,{volume: 0} ).call(()=>{
                AudioManager.instance.audioSource.stop();
            }).start();
        }
    }

    public static fadeinBgm()
    {
        if(!AudioManager.instance.audioSource.playing && AudioManager.sndOn)
        {
            AudioManager.instance.audioSource.volume=0;
            AudioManager.instance.audioSource.play();
            tween(AudioManager.instance.audioSource)
                .to(0.5,{volume: 0.5} ).call(()=>{
            }).start();
        }
    }

    //停止背景音乐
    public static stopBgm()
    {
        AudioManager.instance.audioSource.stop();
    }

    public static isBgmPlaying():boolean
    {
        return AudioManager.instance.audioSource.playing;
    }

    public static stopAll()
    {
        //cc.audioEngine.stopAll();
    }
}
